﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZPod.MTG.Communication.Web;
using System.ServiceModel;
using System.Configuration;
using System.Diagnostics;

namespace ZPod.MTG.WebServer
{
	public class Program
	{
		public static void Main(string[] args)
		{
			string port = ConfigurationManager.AppSettings["Port"];
			string serverName = ConfigurationManager.AppSettings["ServerName"];
			string debug = ConfigurationManager.AppSettings["debug"];
			if (Convert.ToBoolean(debug))
			{
				Debugger.Launch();
			}

			if (String.IsNullOrEmpty(port))
			{
				port = "1234"; //default port
			}

			if (String.IsNullOrEmpty(serverName))
			{
				serverName = "GameServer"; //default name
			}

			string address = String.Format("http://localhost:{0}/{1}/", port, serverName);

			try
			{
				ServiceHostHelper.HostService(
					typeof(GameServerService),
					typeof(IGameServerService),
					new Uri(address));

				Console.ForegroundColor = ConsoleColor.Cyan;
				Console.Write("Servidor iniciado en ");
				Console.ForegroundColor = ConsoleColor.Green;
				Console.WriteLine(address);
				Console.ResetColor();
				Console.WriteLine("Presione cualquier tecla para finalizar...");
				Console.ReadKey();
			}
			catch
			{
				Console.WriteLine("No se pudo iniciar el servidor. Verifique que el puerto esté disponible.");
			}
		}
	}
}
